package com.me.mygdxgame.screens;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.me.mygdxgame.assets.Assets;

public class LoadScreen extends AbstractGameScreen{
	
	TextureAtlas loadScreen;
	TextureRegion backgroud_LoadScreen;
	TextureRegion bar_Load;
	TextureRegion frame_load;
	Texture textLoading;
	TextureRegion[] loads;
	Animation loadingText;
	
	float stateTime = 0;
	int width_Bar;
	OrthographicCamera camera;
	SpriteBatch batch;

	public LoadScreen(Game game) {
		super(game);
	}
	
	

	@Override
	public void show() {
		super.show();
		loadScreen = Assets.loadTextureAtlas("load/loadScreen.pack");
//		backgroud_LoadScreen = loadScreen.findRegion("image 1");
		bar_Load = loadScreen.findRegion("loading-full");
		frame_load = loadScreen.findRegion("frameloading");
		width_Bar = 0;
		textLoading = Assets.loadTexture("load/loading.png");
		textLoading.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		loads = TextureRegion.split(textLoading, 164, 34)[0];
		loadingText = new Animation(0.3f, loads);
		
		camera = new OrthographicCamera(512, 512);
		camera.position.set(512/2, 512/2, 0);
		batch = new SpriteBatch();
	}
	
	public void update(float deltaTime){
		if (deltaTime > 0.1f) deltaTime = 0.1f;
		stateTime += deltaTime;
		if(bar_Load.getRegionWidth() > width_Bar )
			width_Bar += 1;
		else{
			if(Assets.instance.assetManager.update()){
				Assets.instance.takeResources();
				game.setScreen(new MenuScreen(game));
				
			}
		}
		
	}
	
	@Override
	public void dispose() {
		batch.dispose();
		loadScreen.dispose();
		textLoading.dispose();
	}



	public void draw(float deltaTime){
		GLCommon glCommon = Gdx.gl;
		glCommon.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		camera.update();
		batch.setProjectionMatrix(camera.combined);
		batch.enableBlending();
		batch.begin();
//		batch.draw(backgroud_LoadScreen, 0, 0);
		batch.draw(frame_load, 93, 80);
		batch.draw(bar_Load, 94f, 80, width_Bar, 35);
		TextureRegion temp = loadingText.getKeyFrame(stateTime, true);
		batch.draw(temp, (512 - 164)/2, 115);
		batch.end();
	}
	
	@Override
	public void render(float delta) {
		super.render(delta);
		update(delta);
		draw(delta);
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		super.resize(width, height);
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
	}

}
